﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Percolation.Gui
{
    public partial class EditGraphicForm : Form
    {
        public EditGraphicForm(Graphic graphic, ExperimentsRange [] rangeArray)
        {
            InitializeComponent();
            m_rangeArray = rangeArray;
            m_graphic = graphic;
            fillComboBox();
            fillRangeList();
        }

        private ExperimentsRange[] m_rangeArray;
        private Graphic m_graphic;

        private void fillComboBox()
        {
            m_typeComboBox.Items.Clear();
            foreach (GraphicTypeEnum graphicType in System.Enum.GetValues(typeof(GraphicTypeEnum)))
                m_typeComboBox.Items.Add(EnumUtils.GetString(graphicType));

            m_typeComboBox.SelectedIndex = (int)m_graphic.Type;
        }

        private void fillRangeList()
        {
            m_rangeCheckedListBox.Items.Clear();
            for (int i = 0; i < m_rangeArray.Count(); ++i)
            {
                m_rangeCheckedListBox.Items.Add(m_rangeArray[i].GetDescription());
                m_rangeCheckedListBox.SetItemCheckState(i, m_graphic.RangeNums.Contains(i) ? CheckState.Checked : CheckState.Unchecked);
            }
        }

        public Graphic GetGraphic()
        {
            return m_graphic;
        }

        private void m_okButton_Click(object sender, EventArgs e)
        {
            bool canExit = (m_rangeCheckedListBox.CheckedIndices.Count > 0);
            if (!canExit)
                return;

            m_graphic.Type = (GraphicTypeEnum) m_typeComboBox.SelectedIndex;
            m_graphic.RangeNums.AddRange(m_rangeCheckedListBox.CheckedIndices.Cast<int>());

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void m_cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }
    }
}
